package com.yan.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.yan.domain.Customer;
import com.yan.domain.LinkMan;
import com.yan.utils.HibernateUtils;

public class Demo1 {
	
	
	@Test
	public void run4() {
		/**
		 * 查询
		 */
		Session session = HibernateUtils.getCurrentSession();
		Transaction tr = session.beginTransaction();
		
		Customer customer = session.get(Customer.class, 1L);
		
		
		
		tr.commit();
		
	}
	
	@Test
	public void run3() {
		/**
		 * 双向级联保存？
		 */
		Session session = HibernateUtils.getCurrentSession();
		Transaction tr = session.beginTransaction();
		
		Customer c1 = new Customer();
		c1.setCust_name("小美");
		
		LinkMan l1 = new LinkMan();
		LinkMan l2 = new LinkMan();
		
		l1.setLkm_name("熊大");
		l2.setLkm_name("熊二");
		

		l1.setCustomer(c1);
		l2.setCustomer(c1);
		

		session.save(l1);
		session.save(l2);
		
		
		tr.commit();
		
	}
	
	
	@Test
	public void run2() {
		/**
		 * 级联保存
		 */
		Session session = HibernateUtils.getCurrentSession();
		Transaction tr = session.beginTransaction();
		
		Customer c1 = new Customer();
		c1.setCust_name("小美");
		
		LinkMan l1 = new LinkMan();
		LinkMan l2 = new LinkMan();
		
		l1.setLkm_name("熊大");
		l2.setLkm_name("熊二");
		
		c1.getLinkmans().add(l1);
		c1.getLinkmans().add(l2);
		
		l1.setCustomer(c1);
		l2.setCustomer(c1);
		
		
		session.save(c1);
		
		
		tr.commit();
		
	}
	
	
	@Test
	public void run1() {
		/**
		 * 双向关联
		 */
		Session session = HibernateUtils.getCurrentSession();
		Transaction tr = session.beginTransaction();
		
		Customer c1 = new Customer();
		c1.setCust_name("小美");
		
		LinkMan l1 = new LinkMan();
		LinkMan l2 = new LinkMan();
		
		l1.setLkm_name("熊大");
		l2.setLkm_name("熊二");
		
		c1.getLinkmans().add(l1);
		c1.getLinkmans().add(l2);
		
		l1.setCustomer(c1);
		l2.setCustomer(c1);
		
		
		session.save(c1);
		session.save(l1);
		session.save(l2);
		
		
		tr.commit();
		
	}

}
